<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis;

class CpcConfig extends Model
{
	protected $fillable = [
		'ad_provider_id',
		'ad_position_id',
		'rank',
		'key',
		'value',
		'action'
	];
	public $configKeyMap = [
		'min_show_daily'	=>	'每天展现总量至少达到',
		'min_show_hourly'	=>	'每小时展现总量至少达到',
		'max_show_per_device_total'	=>	'每个设备最多展现次数',
		'max_show_per_device_daily'	=>	'每个设备每天最多展现次数'
	];

	const CPC_DEVICE_DAILY_SHOW = "cpc:device:daily:show:";
	const CPC_PROVIDER_DAILY_SHOW = "cpc:provider:daily:show:";
	const CPC_PROVIDER_POSTION_SHOW_HOURLY = "cpc:provider:position:hourly:show:";

	const STATUS_ONLINE = 0;
	const STATUS_OFFLINE = -1;
	private $target = '';

	public function adPositions()
	{
		return $this->belongsTo('\App\Model\AdPosition', 'ad_position_id', 'id');
	}

	public function checkMinShowDaily($deviceId)
	{
		$dailyShowKey = self::CPC_PROVIDER_DAILY_SHOW . date('Ymd') .":{$this->ad_provider_id}";
		$redis = Redis::connection('default');
		if ($redis->get($dailyShowKey) < $this->value) {
			$redis->incr($dailyShowKey);
			$providerIdentifier = $this->adProvider->identifier;
			$this->setTarget($providerIdentifier);
			return true;
		} else {
			return false;
		}
	}

	public function checkMaxShowPerDeviceDaily($deviceId)
	{
		$dailyDeviceShowKey = self::CPC_DEVICE_DAILY_SHOW . date('Ymd');
		$redis = Redis::connection('default');

		if (intval($redis->hGet($dailyDeviceShowKey, $deviceId)) < $this->value) {
			$redis->hIncrBy($dailyDeviceShowKey, $deviceId, 1);
			$providerIdentifier = $this->adProvider->identifier;
			$this->setTarget($providerIdentifier);
			return true;
		} else {
			return false;
		}
	}

	public function checkMinShowHourly($deviceId)
	{
		$providerPositionHourlyShowKey = self::CPC_PROVIDER_POSTION_SHOW_HOURLY . date('YmdH');
		$redis = Redis::connection('default');
		$hashKey = "{$this->ad_provider_id}:{$this->ad_position_id}";

		if ($redis->hGet($providerPositionHourlyShowKey, $hashKey) < $this->value) {
			$currentCount = $redis->hIncrBy($providerPositionHourlyShowKey, $hashKey, 1);
			\Log::info("{$this->ad_provider_id}\t{$this->ad_position_id}\t{$currentCount}");
			if ($currentCount >= $this->value) {
				$redis->hSet($providerPositionHourlyShowKey, "condition_reached_time:{$this->ad_provider_id}", time());
			}
			$providerIdentifier = $this->adProvider->identifier;
			$this->setTarget($providerIdentifier);
			return true;
		} else {
			return false;
		}
	}

	public function checkSortByCtr()
	{
		// \Log::info('checkSortByCtr');
		$adPostionId = $this->ad_position_id;
		$samePositionConfigs = self::with(['adProvider'])->where('ad_position_id', $adPostionId)->get();
		$identifers = [];
		foreach ($samePositionConfigs as $key => $value) {
			$identifers[] = $value->adProvider->identifier;
		}
		$identifers = array_unique($identifers);
		return \App\Service\Ctr::getBestFromKLog($adPostionId, $identifers);
	}

	public function getTarget()
	{
		return $this->target;
	}

	public function setTarget($target)
	{
		$this->target = $target;
	}

	public function adProvider()
	{
		return $this->belongsTo('\App\Model\AdProvider');
	}
}